#!/bin/sh
#
# DracoPKG
#
# Copyright 2008 Ole Andre Rodlie <olear@dracolinux.org>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function dP_audit () {
  INPUT=${1}
  case "${INPUT}" in
    "update")
      if [ -x ${BOOTSTRAP_AUDIT_DOWNLOAD} ]; then
        ${BOOTSTRAP_AUDIT_DOWNLOAD}
      fi
      if [ -a ${DRACOSRC_CACHE}/FILELIST.TXT ]; then
        rm -f ${DRACOSRC_CACHE}/FILELIST.TXT
      fi
      ( cd ${DRACOSRC_CACHE} ; ${GET} ${MIRROR}/draco-${VERSION}/draco/FILELIST.TXT )
    ;;
    "packages")
      ${BOOTSTRAP_AUDIT_PACKAGES}
    ;;
    "system")
      if [ -a ${DRACOSRC_CACHE}/FILELIST.TXT ]; then
        REMOTE=`cat ${DRACOSRC_CACHE}/FILELIST.TXT | awk '{print $8}' | sed 's#/# #g;s#.tgz##g' | awk '{print $3}'`
        for pkg in ${REMOTE}; do
          LOCAL=`( cd ${DRACOSRC_LOG} ; ls | sed '/'${pkg}'/!d')`
          if [ ! "${LOCAL}" ]; then
            PKG_CATEGORY=`cat ${DRACOSRC_CACHE}/FILELIST.TXT | sed '/'${pkg}'/!d' | awk '{print $8}' | sed 's#/# #g;s#.tgz##g' | awk '{print $2}'`
            echo "* Remote package [${PKG_CATEGORY}] ${pkg} not installed."
          fi
        done
        LOCAL=`( cd ${DRACOSRC_LOG} ; ls )`
        for pkg in ${LOCAL}; do
          REMOTE=`cat ${DRACOSRC_CACHE}/FILELIST.TXT | awk '{print $8}' | sed 's#/# #g;s#.tgz##g' | awk '{print $3}' | sed '/'${pkg}'/!d'`
          if [ ! "${REMOTE}" ]; then
            echo "* Local package ${pkg} not in package list."
          fi
        done
        for dotnew in $(find /etc -type f -name "*.new" -maxdepth 2) ; do
          echo "New file found: ${dotnew}"
        done
        for dotnew in $(find /usr/local/etc -type f -name "*.new" -maxdepth 2) ; do
          echo "New file found: ${dotnew}"
        done        
      else
        echo
        echo "Package list not found."
        echo "Please run '${DPN} audit update'."
        echo
        exit 1
      fi      
    ;;
    *)
      echo
      echo "Valid options: [update] [packages] [system]"
      echo
      exit 1
    ;;
  esac
}
